package com.bbm.communitysystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Community;
import com.bbm.communitysystem.domain.dto.CommunityDto;
import com.bbm.communitysystem.domain.vo.InfoCommunityVo;
import com.bbm.communitysystem.domain.vo.PageVo;
import com.bbm.communitysystem.service.ICommunityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-13
 */
@Tag(name = "小区")
@RestController
@RequestMapping("/community")
@RequiredArgsConstructor
public class CommunityController {

    private final ICommunityService communityService;

    @Operation(summary = "新增小区")
    @PostMapping("/")
    public R addCommunity(@RequestBody CommunityDto communityDto) {

        Community community = BeanUtil.copyProperties(communityDto, Community.class);

        community.setUpdateTime(System.currentTimeMillis() + "");

        boolean save = communityService.save(community);

        if (!save) {
            return R.error("新增失败");
        }

        return R.ok("新增成功");
    }

    @Operation(summary = "获取小区详情,暂时无vo", description = "暂时无vo")
    @GetMapping("/{id}")
    public R getCommunity(@PathVariable("id") Integer id) {

        Community byId = communityService.getById(id);

        return R.ok(byId);
    }

    @Operation(summary = "查询小区列表， select页面对象")
    @GetMapping("/list/select")
    public R<PageVo<InfoCommunityVo>> getCommunitySelectList(@RequestParam("page") Integer page,
                                                     @RequestParam("size") Integer size) {

        LambdaQueryWrapper<Community> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Community::getStatus, 1);

        Page<Community> communityPage = new Page<>(page, size);

        Page<Community> page1 = communityService.page(communityPage, queryWrapper);

        PageVo<InfoCommunityVo> pageVo = new PageVo<>(page1);

        page1.getRecords().forEach(item -> {

            InfoCommunityVo infoCommunityVo = BeanUtil.copyProperties(item, InfoCommunityVo.class);
            infoCommunityVo.setKey(item.getId());
            pageVo.getRecords().add(infoCommunityVo);
        });


        return R.ok(pageVo);
    }

    @Operation(summary = "获取小区列表暂时无vo")
    @GetMapping("/list")
    public R getCommunityList(@RequestParam("page") Integer page,
                              @RequestParam("size") Integer size) {
        return R.ok(communityService.list());
    }
    @Operation(summary = "根据id删除小区")
    @DeleteMapping("/{id}")
    public R deleteCommunity(@PathVariable("id") Integer id) {

        boolean removeById = communityService.removeById(id);

        if (!removeById) {
            return R.error("删除失败");
        }

        return R.ok("删除成功");
    }

    @Operation(summary = "修改小区信息")
    @PutMapping("/")
    public R putCommunity(@RequestBody CommunityDto communityDto) {

        boolean update = communityService.lambdaUpdate()
                .set(!communityDto.getCommunityName().isEmpty(), Community::getCommunityName, communityDto.getCommunityName())
                .set(!communityDto.getCommunityContact().isEmpty(), Community::getCommunityContact, communityDto.getCommunityContact())
                .set(!communityDto.getCommunityAddr().isEmpty(), Community::getCommunityAddr, communityDto.getCommunityAddr())
                .set(communityDto.getStatus() != null, Community::getStatus, communityDto.getStatus())
                .set(communityDto.getPropertyId() != null, Community::getPropertyId, communityDto.getPropertyId())

                .set(Community::getUpdateBy, communityDto.getUpdateBy())
                .set(Community::getUpdateTime, System.currentTimeMillis() + "")
                .eq(Community::getId, communityDto.getId())
                .update();

        if (!update) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

}
